package com.shopManager.contoller.shopReduce;

import java.text.ParseException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shopReduce.Enum.ReduceType;
import com.shopReduce.bean.ReduceProgramme;
import com.shopReduce.feign.ReduceProgrammeFeign;

import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;

@RequestMapping("/reduceProgrammeManager/")
@Controller
public class ReduceProgrmmeManagerContoller {
	@Autowired
	ReduceProgrammeFeign reduceProgrammeFeign;
	@RequestMapping("/list.shtml")
	@ResponseBody
	public RestResult index( ReduceProgramme reduceProgramme) {
		
		return reduceProgrammeFeign.list(reduceProgramme);
	}
	@RequestMapping("/insert.shtml")
	@ResponseBody
	public RestResult insert( ReduceProgramme reduceProgramme) throws ParseException {
		if(StringUtils.isBlank(reduceProgramme.getName())) {
			return RestResultGenerator.genErrorResult("优惠方案名称不能为空", null, Status.name_is_empty);
		}
		if(StringUtils.isBlank(reduceProgramme.getParm1()) ||StringUtils.isBlank(reduceProgramme.getParm2())) {
			return RestResultGenerator.genErrorResult("参数1和参数2不能为空", null, Status.parm_is_empty);
		}
		if(reduceProgramme.getType() == null) {
			return RestResultGenerator.genErrorResult("优惠方案类型不能为空", null, Status.type_is_empty);
		}
		if(ReduceType.discount.equals(reduceProgramme.getType())) {
			if(Integer.valueOf(reduceProgramme.getParm1())<=0 ) {
				return RestResultGenerator.genErrorResult("如果是打折，参数1的不能小于0", null, Status.parm_is_error);
			}
			if(Integer.valueOf(reduceProgramme.getParm2())>=10 || Integer.valueOf(reduceProgramme.getParm2())<=0) {
				return RestResultGenerator.genErrorResult("如果是打折，参数2的折扣不能大于10并且不能小于0", null, Status.parm_is_error);
			}
		}else {
			if(Integer.valueOf(reduceProgramme.getParm1())<Integer.valueOf(reduceProgramme.getParm2()) ) {
				return RestResultGenerator.genErrorResult("如果是满减，参数2的不能大于参数参数1", null, Status.parm_is_error);
			}
		}
		if(StringUtils.isBlank(reduceProgramme.getBeignDate())) {
			return RestResultGenerator.genError("优惠开始时间不能为空");
		}
		if(StringUtils.isBlank(reduceProgramme.getExpireDate())) {
			return RestResultGenerator.genError("优惠过期时间不能为空");
		}
		if(DateUtils.parseDate(reduceProgramme.getExpireDate(), "yyyy-MM-dd HH:mm:ss").getTime()<=DateUtils.parseDate(reduceProgramme.getBeignDate(), "yyyy-MM-dd HH:mm:ss").getTime()) {
			return RestResultGenerator.genError("优惠开始时间不能小于过期时间");
		}
		
		RestResult result = reduceProgrammeFeign.insert(reduceProgramme);
		if(result == null) {
			return RestResultGenerator.genError("添加失败，服务不可用");
		}
		return result;
	}
	
	@RequestMapping("addInit.shtml")
	public String addInit(String id,Model model) {
		if(StringUtils.isNotBlank(id)) {
			model.addAttribute("reduce", reduceProgrammeFeign.getApi(id).getData());
		}
		return "reduceProgramme/add";
	}
	


	
	
}
